<template>
  <transition name="slide">
    <div class="fm_player">
      <div class="fm">
        <div class="title_grp">
          <div class="fm_title">
            {{currentFm.title}}
          </div>
          <p>来源：{{currentFm.author}}</p>
        </div>

        <div class="fm_control">
          <div class="img_grp">
            <div class="img_grp2">
              <img :src="currentFm.pic_radio" alt="">
            </div>
            <div class="img_grp2">
              <img :src="currentFm.pic_radio" alt="">
            </div>
            <div class="img_grp2">
              <img :src="currentFm.pic_radio" alt="">
            </div>
          </div>

          <div class="control">
            <progress-bar :percent="percent" @percentChange="onProgressBarChange"></progress-bar>
            <div class="time_grp">
              <span class="now_time">{{format(currentTime)}}</span>
              <span class="all_time">{{format(duration)}}</span>
            </div>
          </div>

          <div class="iconfont_grp">
            <span class="iconfont icon-shoucang"></span>
            <span class="iconfont icon-last-icon-copy" @click="prev"></span>
            <span class="iconfont " :class="playIcon" @click="togglePlaying"></span>
            <span class="iconfont icon-next-icon" @click="next"></span>
            <span class="iconfont icon-share-icon"></span>
          </div>
        </div>
      </div>
      <div class="more" @click="handlshowList">
        <span class="iconfont icon-list-icon"></span>
        <span class="look_more">查看更多</span>
        <span class="iconfont icon-getmore-icon"></span>
      </div>
      <audio id="fm" :src="bitrate.file_link"
             ref="audio" @canplay="ready" @error="error" @timeupdate="updateTime"></audio>
      <list :fmList="fmList" :song_id="Number(currentFm.song_id)" :refresh="showList" v-show="showList"
            @close="closeList" @selectFm="selectFm"></list>
      <div class="shadow" v-if="showList"></div>
    </div>
  </transition>
</template>

<script>
  import ProgressBar from './components/progress-bar'
  import List from './components/list'

  export default {
    name: 'FmPlayer',
    created() {
      this.touch = {}
    },

    data() {
      return {
        songReady: false,
        currentTime: 0,
        currentFm: '',
        bitrate: '',
        isPlaying:false,
        fid: this.$route.params.fid,
        nid: this.$route.params.fmtid,
        showList: false
      }
    },

    watch: {
      playing(newPlaying) {
        const audio = this.$refs.audio
        newPlaying ? audio.play() : audio.pause()
      },
      percent() {
        if (this.percent == 1) {
          this.next()
        }
      }
    },

    computed: {
      fmList() {
        let fml = this.$store.state.fmList
        if (fml.length == 0) {
          this.loadFmList()
        }
        return this.$store.getters.getfmList
      },
      duration() {
        if (this.songReady) {
          return this.$refs.audio.duration
        }
      },
      percent() {
        return this.currentTime / this.duration
      },
      playIcon() {
        return this.playing ? 'icon-parse-icon' : 'icon-play-icon'
      },

      playing() {
        return this.$store.getters.getPlaying
      },
      disableCls() {
        return this.songReady ? '' : 'disable'
      }
    },

    mounted() {
      let _this = this
      this.getFm(this.fid)
      this.isPlaying = true
      window.addEventListener("popstate", function (e) {
         if(_this.isPlaying){
           _this.showList = false
           _this.$router.push({path: `/psyFm`})
         }
      }, false);

    },


    methods: {
      handlshowList() {
        this.showList = true
      },
      loadFmList() {
        this.axios.get(`/v1/restserver/ting?format=json&method=baidu.ting.billboard.billList&type=${this.nid}&size=6&offset=0`).then((res) => {
          this.$store.commit('changeFmList', res.data.song_list)
        })
      },

      currentIndex(song_id) {
        let list = this.fmList
        let index = list.findIndex((arr, value, index) => {
          if (arr.song_id == song_id) {
            return index
          }
        })
        this.$store.commit('setCurrentIndex', {currentIndex: index});
      },

      selectFm(song_id){
        this.getFm(song_id)
      },
      getFm(fid) {
        let _this = this
        this.axios.get(`/v1/restserver/ting?format=json&method=baidu.ting.song.play&songid=${fid}`).then((res) => {
          this.currentFm = res.data.songinfo
          this.bitrate = res.data.bitrate
          setTimeout(function () {
            _this.currentIndex(_this.currentFm.song_id)
            _this.canPlay()
          }, 700)
        })
      },
      canPlay() {
        // let wx = require('weixin-js-sdk');
        const audio = this.$refs.audio
        audio.play();
        this.$store.commit('setPlaying', {playing: true})
        // WeixinJSBridge.invoke('getNetworkType',{},function (res) {
        //
        // })
      },
      ready() {
        this.songReady = true
        // this.savePlayHistory(this.currentSong)
      },
      updateTime(e) {
        this.currentTime = e.target.currentTime
        // console.log(this.currentTime)
      },
      format(interval) {
        interval = interval | 0
        const minute = interval / 60 | 0
        const second = this._pad(interval % 60)
        return `${minute}:${second}`
      },
      _pad(num, n = 2) {
        let len = num.toString().length
        while (len < n) {
          num = '0' + num
          len++
        }
        return num
      },
      error() {
        this.songReady = true
      },
      next() {
        if (!this.songReady) {
          return
        }

        let index = this.$store.state.currentIndex + 1
        if (index === this.$store.state.fmList.length) {
          index = 0
        }
        let song_id = this.fmList[index].song_id
        this.getFm(song_id)
        this.$router.push({path: `/fmPlayer/${this.nid}/${song_id}`})
        if (!this.playing) {
          this.togglePlaying()
        }
        this.songReady = false
      },
      prev() {
        if (!this.songReady) {
          return
        }
        let index = this.$store.state.currentIndex - 1
        if (index === -1) {
          index = this.$store.state.fmList.length - 1
        }
        let song_id = this.fmList[index].song_id
        this.getFm(song_id)
        this.$router.push({path: `/fmPlayer/${this.nid}/${song_id}`})
        if (!this.playing) {
          this.togglePlaying()
        }
        this.songReady = false
      },
      togglePlaying() {
        if (!this.songReady) {
          return
        }
        let playing = this.$store.state.playing
        this.$store.commit('setPlaying', {playing: !playing})
        // this.songReady = false;
        console.log(this.$store.state.playing)
      },
      onProgressBarChange(percent) {
        const currentTime = this.$refs.audio.duration * percent
        this.$refs.audio.currentTime = currentTime
        if (!this.playing) {
          this.togglePlaying()
        }

      },
      closeList() {
        this.showList = false
      }
    },
    activated() {
      this.songReady = false
      this.$store.commit('setPlaying', {playing: false})
      this.fid = this.$route.params.fid
      this.getFm(this.fid)

    },

    destroyed(){
      this.isPlaying = false
    },
    components: {
      ProgressBar,
      List
    }

  }
</script>


<style scoped>

  .fm_player {
    position: fixed;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    z-index: 20;
    background-color: #f5f4f7;
  }

  .disableCls {
    opacity: 0.5;
  }

  .fm {
    background-color: white;
    padding: 40px 26px 30px 26px;
  }

  .title_grp {
    height: 160px;
  }

  .fm_title {
    font-size: 42px;
    line-height: 60px;
    color: black;
    font-weight: bold;
    /*text-align: center;*/
  }

  p {
    font-size: 24px;
    color: #999;
    padding-left: 4px;
    /*text-align: center;*/
    margin: 0;
  }

  .fm_control {
    padding: 10px 42px;
  }

  .img_grp {
    width: 615px;
    height: 658px;
    position: relative;
    overflow: hidden;
  }

  .img_grp2 {
    border-radius: 20px;
    /*border: solid 1px rebeccapurple;*/
    overflow: hidden;
  }

  .img_grp2:nth-of-type(1) {
    width: 557px;
    height: 557px;
    opacity: 0.2;
    margin: 0 auto;
  }

  .img_grp2:nth-of-type(2) {
    width: 590px;
    height: 590px;
    opacity: 0.4;
    position: absolute;
    left: 12px;
    top: 20px;
  }

  .img_grp2:nth-of-type(3) {
    width: 615px;
    height: 615px;
    position: absolute;
    left: 0;
    top: 40px;
  }

  .control {
    width: 100%;
    height: 50px;
    margin-top: 43px;
  }

  .basebar {
    height: 4px;
    border-radius: 2px;
    background-color: #cecece;
    position: relative;
  }

  .time_grp {
    margin-top: 10px;
  }

  .time_grp span {
    font-size: 20px;
    color: #999;
  }

  .all_time {
    float: right;
    margin-top: 6px;
  }

  .iconfont_grp {
    height: 100px;
    /*line-height: 20px;*/
    text-align: center;
  }

  .iconfont_grp .iconfont {
    float: left;
  }

  .icon-play-icon {
    font-size: 100px;
    color: #059692;
    margin: 0 80px;
  }

  .icon-parse-icon {
    font-size: 100px;
    color: #059692;
    margin: 0 80px;
  }

  .icon-next-icon {
    font-size: 40px;
    color: #333;
    float: right;
    margin: 28px 0;
  }

  .icon-last-icon-copy {
    font-size: 40px;
    color: #333;
    float: right;
    margin: 28px 0;
  }

  .iconfont_grp .icon-share-icon {
    font-size: 36px;
    color: #999;
    float: right;
    margin-top: 33px;
  }

  .icon-shoucang {
    float: left;
    font-size: 34px;
    color: #999;
    margin-top: 33px;
    margin-right: 104px;
  }

  .more {
    height: 96px;
    background-color: #f5f4f7;
    line-height: 96px;
    width: 100%;
    padding: 0 26px;
  }

  .icon-list-icon {
    font-size: 36px;
    color: #999;
    margin-top: 4px;
    float: left;
    margin-right: 10px;
  }

  .look_more {
    font-size: 24px;
    color: #666666;
  }

  .icon-getmore-icon {
    font-size: 32px;
    color: #999;
    float: right;
  }

  header {
    text-align: center;
    height: 96px;
    background-color: #f5f4f7;
    padding: 32px 26px;
    font-size: 36px;
    font-weight: bold;
    line-height: 1;
    position: relative;
  }

  .icon-close-icon {
    position: absolute;
    right: 26px;
    top: 30px;
    font-size: 30px;
    color: #999;
  }

  ul {
    height: 675px;
    padding: 0 26px;
  }

  li .iconfont {
    float: left;
    font-size: 58px;
    margin-right: 26px;
    margin-top: 8px;
  }

  li .icon-playing {
    color: #ff6d00;
  }

  li {
    border-bottom: solid 1px #ececec;
    height: 112px;
    padding: 16px 0;
  }

  li:last-of-type {
    border-bottom: none;
  }

  .fm_content {
    width: auto;
  }

  .fm_content .title {
    font-size: 28px;
    color: #333;
  }

  .select .title {
    color: #ff6d00;
  }

  .fm_content span {
    font-size: 20px;
    color: #999;
  }

  .slide-enter-active, .slide-leave-active {
    transition: all 0.3s

  }

  .slide-enter, .slide-leave-to {
    transform: translate3d(100%, 0, 0)

  }

</style>
